﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.Profile;
using System.Net.Mail;

namespace Continental.UI.Member
{
    public partial class Login : System.Web.UI.Page
    {

        public String message = "";
        public String message2 = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Cookies["haiphongclick_password"] != null && Request.Cookies["haiphongclick_email"] != null)
                {
                    this.txtEmail.Attributes.Add("value", Request.Cookies["haiphongclick_email"].Value);
                    this.txtPassword.Attributes.Add("value", Request.Cookies["haiphongclick_password"].Value);
                    this.ckRemember.Checked = true;
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Page.Validate("Login");
            if (Page.IsValid)
            {
                Boolean flag = Membership.ValidateUser(this.txtEmail.Text.Trim(), this.txtPassword.Text.Trim());
                if (!flag)
                {
                    message = "Tài khoản không hợp lệ";
                }
                else
                {
                    if (this.ckRemember.Checked)
                    {
                        Response.Cookies["haiphongclick_password"].Value = this.txtPassword.Text.Trim();
                        Response.Cookies["haiphongclick_password"].Expires = DateTime.Now.AddDays(7);

                        Response.Cookies["haiphongclick_email"].Value = this.txtEmail.Text.Trim();
                        Response.Cookies["haiphongclick_email"].Expires = DateTime.Now.AddDays(7);
                    }
                    else
                    {
                        Response.Cookies["haiphongclick_password"].Expires = DateTime.Now.AddDays(-1);
                        Response.Cookies["haiphongclick_email"].Expires = DateTime.Now.AddDays(-1);
                    }
                    FormsAuthentication.RedirectFromLoginPage(this.txtEmail.Text, false);
                }
            }
        }

        protected void btnForgetPWD_Click(object sender, EventArgs e)
        {
            Page.Validate("ForgetPwd");
            if (Page.IsValid)
            {
                String email = this.txtEmail2.Text.Trim();
                MembershipUser theUser = Membership.GetUser(email);
                if (theUser == null)
                {
                    message2 += "Email không tồn tại trong hệ thống";
                    return;
                }
                else
                {
                    try
                    {
                        String adminEmail = ConfigurationManager.AppSettings["AdminEmail"].ToString();
                        String password = ConfigurationManager.AppSettings["Password"].ToString();
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                        //thiet lap cac thong so cho viec reset
                        String thePasswordForReset = DateTime.Now.Ticks.ToString();
                        thePasswordForReset = thePasswordForReset.Substring(thePasswordForReset.Length - 10);
                        ProfileBase theProfile = ProfileBase.Create(theUser.UserName);
                        theProfile.SetPropertyValue("PasswordForReset", thePasswordForReset);
                        theProfile.SetPropertyValue("Time", DateTime.Now.AddDays(1));
                        theProfile.Save();

                        // thiet lap email
                        mail.From = new MailAddress(adminEmail);
                        mail.To.Add(this.txtEmail2.Text.Trim());
                        mail.Subject = "Test Mail";
                        String theUrl = "http://localhost:49323/Thanh-Vien/" + this.txtEmail2.Text.Trim() + "/" + thePasswordForReset + "/quen-mat-khau.aspx";
                        mail.Body = "This is for testing SMTP mail from GMAIL : " + theUrl;
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential(adminEmail, password);
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
                        message2 += "Một email đã được gửi đến hòm thư của bạn";
                    }
                    catch (Exception ex)
                    {
                        message2 += "Có lỗi gửi mail : " + ex.Message;
                    }
                }
            }
        }
    }
}
